package com.dexilaonian.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.util.Log;
import android.widget.Toast;

import com.dexilaonian.R;
import com.dexilaonian.activity.SuccessPayActivity;
import com.dexilaonian.utils.Constant;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import de.greenrobot.event.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(WXPayEntryActivity.this, Constant.APP_ID,false);
        api.handleIntent(getIntent(), WXPayEntryActivity.this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.e("TTTTTTTTTTTTonPayFinish", "errCode = " + resp.errCode+resp+"=="+resp.getType());
		int code = resp.errCode;
		switch (code){
			case 0://支付成功后的界面
				Intent intent;
				intent = new Intent(this, SuccessPayActivity.class);
				startActivity(intent);
				//发送通知修改购买界面的值(具体等有接口以后修改)
				//EventBus.getDefault().post(new CourseBuyActivityEvent(Constant.NOTIFY_COURSE_BUY_ACTIVITY));
				break;
			case -1:
			//	Toast.makeText(getApplicationContext(),"签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、您的微信账号异常等。",Toast.LENGTH_SHORT).show();
				Toast.makeText(getApplicationContext(),"-1",Toast.LENGTH_SHORT).show();
				break;
			case -2://用户取消支付后的界面
				Toast.makeText(getApplicationContext(),"取消支付",Toast.LENGTH_SHORT).show();
				break;
		}
		finish();
		//微信支付后续操作，失败，成功，取消

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			AlertDialog.Builder builder = new AlertDialog.Builder(WXPayEntryActivity.this);
			builder.setTitle(R.string.app_tip);
			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
			builder.show();
		}
	}
}